function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function getTotalCalorByAge({
  BMI,
  age,
  waist,
  height,
  gender
}) {
  if (gender == '女') {
    if (18.5 < BMI && BMI < 24) {
      if (waist > 85) {
        return 1400;
      }
      if (waist <= 85) {
        if (height <= 155) {
          return 1400;
        } else if (156 < height && height <= 170) {
          return 1600;
        } else if (170 < height) {
          return 1800;
        }
      }
    } else if (24 <= BMI) {
      return 1400;
    } else if (BMI < 18.5) {
      return 1800; //(建议看医生)
    }
  } else if (gender == '男') {
    if (18.5 < BMI && BMI < 24) {
      if (waist > 90) {
        return 1400;
      }
      if (waist <= 85) {
        if (height <= 165) {
          return 1400;
        } else if (165 < height && height <= 175) {
          return 1600;
        } else if (175 < height) {
          return 1800;
        }
      }
    } else if (24 <= BMI) {
      return 1400;
    } else if (BMI < 18.5) {
      return 2000; //(建议看医生)
    }
  }
}

function calcBmi(weight, height) {
  return parseInt(weight / (height / 100) / (height / 100));
}

// 主调用方法
/**
 * 获取小于限制大小的Image, limitSize默认为1M，递归调用。
 */
function getLessLimitSizeImage(canvasId, imagePath, limitSize = 1, drawWidth, callBack) {
  // 给的文件资源是否小于LimitSize （M）, 小于走lessCallBack， 大于走moreCallBack
  function imageSizeIsLessLimitSize(imagePath, limitSize, lessCallBack, moreCallBack) {
    wx.getFileInfo({
      filePath: imagePath,
      success(res) {
        console.log("压缩前图片大小:", res.size / 1024, 'kb');
        if (res.size > 1024 * 1024 * limitSize) {
          moreCallBack();
        } else {
          lessCallBack();
        }
      }
    })
  }

  // 获取画布图片
  function getCanvasImage(canvasId, imagePath, imageW, imageH, getImgsuccess) {
    const ctx = wx.createCanvasContext(canvasId);
    ctx.drawImage(imagePath, 0, 0, imageW, imageH);
    ctx.draw(false, () => {
      wx.canvasToTempFilePath({
        canvasId: canvasId,
        x: 0,
        y: 0,
        width: imageW,
        height: imageH,
        quality: 1,
        success(res) {
          getImgsuccess(res.tempFilePath);
        }
      });
    });
  }

  imageSizeIsLessLimitSize(imagePath, limitSize,
    (lessRes) => {
      callBack(imagePath);
    },
    (moreRes) => {
      wx.getImageInfo({
        src: imagePath,
        success: function(imageInfo) {
          var maxSide = Math.max(imageInfo.width, imageInfo.height);
          //画板的宽高默认是windowWidth
          var windowW = drawWidth;
          var scale = 1;
          if (maxSide > windowW) {
            scale = windowW / maxSide;
          }
          var imageW = Math.floor(imageInfo.width * scale);
          var imageH = Math.floor(imageInfo.height * scale);
          console.log('调用压缩', imageW, imageH);
          getCanvasImage(canvasId, imagePath, imageW, imageH,
            (pressImgPath) => {
              getLessLimitSizeImage(canvasId, pressImgPath, limitSize, drawWidth * 0.7, callBack);
            }
          );
        }
      })
    }
  )
}

/**
  使用: windowWidth为屏幕宽度
  定义不可见画布
  <canvas
    canvas-id='pressCanvas'
    class='press-canvas'
    style='width: {{ windowWidth }}px; height: {{ windowWidth }}px;position: absolute;top: -1000px;background-color: gray;' ></canvas>
 */

module.exports = {
  formatTime: formatTime,
  calcBmi: calcBmi,
  getTotalCalorByAge: getTotalCalorByAge,
  getLessLimitSizeImage: getLessLimitSizeImage,
}